<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:lxslt="http://xml.apache.org/xslt"
  xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
  extension-element-prefixes="redirect">
  <xsl:output method="html" indent="yes" encoding="US-ASCII"/>
  <xsl:decimal-format decimal-separator="." grouping-separator=","/>
  <!--
  Sample stylesheet to be used with An JUnitReport output.
  It creates a set of HTML files a la javadoc where you can browse easily
  through all packages and classes.
  @author Stephane Bailliez <a href="mailto:sbailliez@apache.org"/>
  @author Erik Hatcher <a href="mailto:ehatcher@apache.org"/>
  -->
  <xsl:param name="output.dir" select="'.'"/>
  
  <xsl:template match="testsuites">
    <!-- create the index.html -->
    <redirect:write file="{$output.dir}/index.html">
      <xsl:call-template name="index.html"/>
    </redirect:write>
    
    <!-- create the stylesheet.css -->
    <redirect:write file="{$output.dir}/stylesheet.css">
      <xsl:call-template name="stylesheet.css"/>
    </redirect:write>
    
    <!-- create the overview-packages.html at the root -->
    <redirect:write file="{$output.dir}/overview-summary.html">
      <xsl:apply-templates select="." mode="overview.packages"/>
    </redirect:write>
    
    <!-- create the all-packages.html at the root -->
    <redirect:write file="{$output.dir}/overview-frame.html">
      <xsl:apply-templates select="." mode="all.packages"/>
    </redirect:write>
    
    <!-- create the all-classes.html at the root -->
    <redirect:write file="{$output.dir}/allclasses-frame.html">
      <xsl:apply-templates select="." mode="all.classes"/>
    </redirect:write>
    
    <!-- process all packages -->
    <xsl:for-each select="*[not(./@packagename = preceding-sibling::*/@packagename)]">
      <xsl:call-template name="package">
	<xsl:with-param name="name" select="@packagename"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>
  
  
  <xsl:template name="package">
    <xsl:param name="name"/>
    <xsl:variable name="package.dir">
      <xsl:if test="not($name = '')"><xsl:value-of select="$name"/></xsl:if>
      <xsl:if test="$name = ''">.</xsl:if>
    </xsl:variable> 
    <!--Processing package <xsl:value-of select="@name"/> in <xsl:value-of select="$output.dir"/> -->
    <!-- create a classes-list.html in the package directory -->
    <redirect:write file="{$output.dir}/{$package.dir}/package-frame.html">
      <xsl:call-template name="classes.list">
	<xsl:with-param name="name" select="$name"/>
      </xsl:call-template>
    </redirect:write>
    
    <!-- create a package-summary.html in the package directory -->
    <redirect:write file="{$output.dir}/{$package.dir}/package-summary.html">
      <xsl:call-template name="package.summary">
	<xsl:with-param name="name" select="$name"/>
      </xsl:call-template>
    </redirect:write>
    
    <!-- for each class, creates a @name.html -->
    <!-- @bug there will be a problem with inner classes having the same name, it will be overwritten -->
    <xsl:for-each select="/testsuites/testcase[@packagename = $name]">
      <xsl:variable name="test.name" select="@name"/>
      <redirect:write file="{$output.dir}/{$package.dir}/{@name}.html">
	<xsl:apply-templates select="." mode="class.details"/>
      </redirect:write>
      
      <xsl:for-each select="/testsuites/task[@packagename = $name and @name = $test.name]">
	<redirect:write file="{$output.dir}/{$package.dir}/{@name}_{@taskname}.html">
	  <xsl:call-template name="task.details">
	    <xsl:with-param name="name" select="$name"/>
	  </xsl:call-template>
	  <!--xsl:apply-templates select="." mode="details"/-->
	</redirect:write>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="index.html">
    <html>
      <head>
        <title>Dant Test Report </title>
      </head>
      <frameset cols="20%,80%">
        <frameset rows="30%,70%">
	  <frame src="overview-frame.html" name="packageListFrame"/>
	  <frame src="allclasses-frame.html" name="classListFrame"/>
        </frameset>
        <frame src="overview-summary.html" name="classFrame"/>
        <noframes>
	  <h2>Frame Alert</h2>
	  <p>
                This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
	  </p>
        </noframes>
      </frameset>
    </html>
</xsl:template>
  
  <!-- this is the stylesheet css to use for nearly everything -->
  <xsl:template name="stylesheet.css">
    body {
    font:normal 68% verdana,arial,helvetica;
    color:#000000;
    }
    table tr td, table tr th {
    font-size: 68%;
    }
    table.details tr th{
    font-weight: bold;
    text-align:left;
    background:#a6caf0;
    }
    table.details tr td{
    background:#eeeee0;
    }

    p {
    line-height:1.5em;
    margin-top:0.5em; margin-bottom:1.0em;
    }
    h1 {
    margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
    }
    h2 {
    margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
    }
    h3 {
    margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
    }
    h4 {
    margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
    }
    h5 {
    margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
    }
    h6 {
    margin-bottom: 0.5em; font: bold 100% verdana,arial,helvetica
    }
    .Skipped {
    color:blue;
    }
.Warning {
    font-weight:bold; color:purple;
    }
    .FAILED {
    font-weight:bold; color:red;
    }
    .Output {
    font-size: 120%;
    }
  </xsl:template>
  

  <!-- ======================================================================
  This page is created for every testcase class.
  It prints a summary of the testcase and detailed information about
  process methods.
  ====================================================================== -->
  <xsl:template match="testcase" mode="class.details">
    <xsl:variable name="package.name" select="@packagename"/>
    <xsl:variable name="test.name" select="@name"/>
    <html>
      <head>
	<title>Dant Test Suites: <xsl:value-of select="$test.name"/></title>
	<xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name" select="$package.name"/>
	</xsl:call-template>
      </head>
      <body>
	<xsl:call-template name="pageHeader"/>  
	<h3>Test <xsl:value-of select="$package.name"/>/<xsl:value-of select="$test.name"/></h3>
	
	<table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
	  <xsl:call-template name="testcase.header.long"/>
	  <xsl:apply-templates select="." mode="print.long">
	    <xsl:sort select="@orderid" order="ascending" data-type="number"/>
	  </xsl:apply-templates>
	</table>
	
	<h2>Test Task(s):</h2>
	<table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
	  <xsl:call-template name="task.header"/>
	  <xsl:apply-templates select="/testsuites/task[./@packagename = $package.name and ./@name = $test.name]" mode="print">
	    <xsl:sort select="@orderid" order="ascending" data-type="number"/>
	  </xsl:apply-templates>
	</table>
      </body>
    </html>
  </xsl:template>

  <xsl:template name="task.details">
    <xsl:param name="name"/>
    <html>
      <head>
	<title><xsl:value-of select="@name"/></title>
	<xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name" select="$name"/>
	</xsl:call-template>
      </head>
      <body>
	<xsl:call-template name="pageHeader"/>  
	<h3>task <xsl:value-of select="@taskname"/> details:</h3>
	<table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
	  <xsl:call-template name="details.table.body"/>  
	</table>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template name="details.table.body">
    <tr valign="top">
      <th width="10%">Test</th>
      <th align="left"><xsl:value-of select="@packagename"/>/<xsl:value-of select="@name"/></th>
    </tr>
    
    <tr valign="top">
      <td>Command</td>
      <td><xsl:value-of select="command"/></td>
    </tr>
    <tr valign="top">
      <td nowrap="nowrap">Duplicated Process(es)</td>
      <td><xsl:value-of select="@duplicate"/></td>
    </tr>
    <tr valign="top">
      <td nowrap="nowrap">Host</td>
      <td><xsl:value-of select="@host"/></td>
    </tr>
    <tr valign="top">
      <td>Summary</td>
      <td><xsl:value-of select="description"/></td>
    </tr>
    <tr valign="top">
      <td>Std Out</td>
      <td><pre class="Output"><xsl:value-of select="systemout"/></pre></td>
    </tr>
    <tr valign="top">
      <td>Std Err</td>
      <td><pre class="Output"><xsl:value-of select="systemerr"/></pre></td>
    </tr>
  </xsl:template>
  
  <!-- ======================================================================
  This page is created for every package.
  It prints the name of all classes that belongs to this package.
  @param name the package name to print classes.
  ====================================================================== -->
  <!-- list of classes in a package -->
  <xsl:template name="classes.list">
    <xsl:param name="name"/>
    <html>
      <head>
	<title>Dant Tests: <xsl:value-of select="$name"/></title>
	<xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name" select="$name"/>
	</xsl:call-template>
      </head>
      <body>
	<table width="100%">
	  <tr>
	    <td nowrap="nowrap">
	      <h2><a href="package-summary.html" target="classFrame"><xsl:value-of select="$name"/></a></h2>
	    </td>
	  </tr>
	</table>
	
	<h2>Test Cases</h2>
	<table width="100%">
	  <xsl:for-each select="/testsuites/testcase[./@packagename = $name]">
	    <xsl:sort select="@orderid" order="ascending" data-type="number"/>
	    <tr>
	      <td nowrap="nowrap">
		<a href="{@name}.html" target="classFrame"><xsl:value-of select="@name"/></a>
	      </td>
	    </tr>
	  </xsl:for-each>
	</table>
      </body>
    </html>
  </xsl:template>

  
  <!--
  Creates an all-classes.html file that contains a link to all package-summary.html
  on each class.
  -->
  <xsl:template match="testsuites" mode="all.classes">
    <html>
      <head>
	<title>All Dant Tests</title>
            <xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name"/>
	</xsl:call-template>
      </head>
      <body>
	
	<h2>Test Cases</h2>
	<table width="100%">
	  <xsl:apply-templates select="testcase" mode="all.classes">
	    <xsl:sort select="@orderid" order="ascending" data-type="number"/>
	  </xsl:apply-templates>
	</table>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="testcase" mode="all.classes">
    <xsl:variable name="package.name" select="@packagename"/>
    <tr>
      <td nowrap="nowrap">
	<a target="classFrame">
	  <xsl:attribute name="href">
	    <xsl:if test="not($package.name='')">
	      <xsl:value-of select="$package.name"/><xsl:text>/</xsl:text>
	    </xsl:if>
	    <xsl:value-of select="@name"/><xsl:text>.html</xsl:text>
	  </xsl:attribute>
	  <xsl:value-of select="@name"/>
	</a>
      </td>
    </tr>
  </xsl:template>
  
  <!--
  Creates an html file that contains a link to all package-summary.html files on
  each package existing on testsuites.
  @bug there will be a problem here, I don't know yet how to handle unnamed package :(
  -->
  <xsl:template match="testsuites" mode="all.packages">
    <html>
      <head>
	<title>All Dant Test Packages</title>
	<xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name"/>
	</xsl:call-template>
      </head>
      <body>
	<h2><a href="overview-summary.html" target="classFrame">Home</a></h2>
	<h2>Test Suites</h2>
	<table width="100%">
	  <xsl:for-each select="/testsuites/*[not(./@packagename = preceding-sibling::*/@packagename)]">
	    <xsl:sort select="@packagename" order="ascending"/>
	    <tr>
	      <td nowrap="nowrap">
		<a href="{current()/@packagename}/package-summary.html" target="classFrame">
		  <xsl:value-of select="current()/@packagename"/>
		</a>
	      </td>
	    </tr>
	  </xsl:for-each>
	</table>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="testsuites" mode="overview.packages">
    <html>
      <head>
	<title>Dant Test Suites: Summary</title>
	<xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name"/>
	</xsl:call-template>
      </head>
        <body>
        <xsl:attribute name="onload">open('allclasses-frame.html','classListFrame')</xsl:attribute>
        <xsl:call-template name="pageHeader"/>
        <h2>Summary</h2>
        <xsl:variable name="testCount" select="count(testcase)"/>
        <xsl:variable name="failTestCount" select="count(testcase[./@result = 'FAILED'])"/>
        <xsl:variable name="testSuccessRate" select="($testCount - $failTestCount) div $testCount"/>
        <xsl:variable name="timeCount" select="sum(task[not(starts-with(@taskname,'startjavaserver')) and not(starts-with(@taskname,'startcxxserver'))]/@time)"/>
        <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
	  <tr valign="top">
            <th style="background:#ffffff;"><xsl:text disable-output-escaping="yes">&amp;</xsl:text>nbsp;</th>
            <th>Total</th>
            <th>Failures</th>
            <th>Success rate</th>
            <th>Time</th>
	  </tr>
	  <tr valign="top">
            <th>Test Cases</th>
            <td><xsl:value-of select="$testCount"/></td>
            <td><xsl:value-of select="$failTestCount"/></td>
            <td>
	      <xsl:call-template name="display-percent">
		<xsl:with-param name="value" select="$testSuccessRate"/>
	      </xsl:call-template>
            </td>
            <td>
	      <xsl:call-template name="display-time">
		<xsl:with-param name="value" select="$timeCount"/>
	      </xsl:call-template>
            </td>
	  </tr>
	  
        </table>
        <table border="0" width="95%">
	  <tr>
	  </tr>
        </table>
	
        <h2>Test Suites</h2>
        <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
	  <xsl:call-template name="testsuites.header"/>
	  <xsl:for-each select="/testsuites/*[not(./@packagename = preceding-sibling::*/@packagename)]">
	    <xsl:sort select="@packagename" order="ascending"/>
	    <!-- get the node set containing all testsuites that have the same package -->
	    <xsl:variable name="insamepackagetests" select="/testsuites/testcase[./@packagename = current()/@packagename]"/>
	    <xsl:variable name="insamepackage" select="$insamepackagetests"/>
	    <tr valign="top">
	      <xsl:attribute name="class">
		<xsl:choose>
		  <xsl:when test="count($insamepackage[./@result = 'FAILED']) &gt; 0">FAILED</xsl:when>
		  <xsl:otherwise>Passed</xsl:otherwise>
		</xsl:choose>
	      </xsl:attribute>
	      <td>
		<xsl:choose>
		  <xsl:when test="@packagename = ''">
		    <a href="package-summary.html">&lt;root&gt;</a>
		  </xsl:when>
		  <xsl:otherwise>
		    <a href="{@packagename}/package-summary.html"><xsl:value-of select="@packagename"/></a>
		  </xsl:otherwise>
		</xsl:choose>
	      </td>

	      <td><xsl:value-of select="count($insamepackagetests)"/></td>
	      
	      <td>
		
		<xsl:value-of select="count($insamepackage[./@result = 'FAILED'])"/>
	      </td>

	      <td>
		<xsl:call-template name="display-time">
		  <xsl:with-param name="value" select="sum(../task[@packagename = current()/@packagename and not(starts-with(@taskname,'startjavaserver')) and not(starts-with(@taskname,'startcxxserver'))]/@time)"/>
		</xsl:call-template>
	      </td>
	    </tr>
	  </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>


  <xsl:template name="package.summary">
    <xsl:param name="name"/>
    <html>
        <head>
	<xsl:call-template name="create.stylesheet.link">
	  <xsl:with-param name="package.name" select="$name"/>
	</xsl:call-template>
      </head>
      <body>
	<xsl:attribute name="onload">open('package-frame.html','classListFrame')</xsl:attribute>
	<xsl:call-template name="pageHeader"/>
	<h3>Test Suite <xsl:value-of select="$name"/></h3>
	
	<xsl:variable name="insamepackagetests" select="/testsuites/testcase[./@packagename = $name]"/>
	<xsl:if test="count($insamepackagetests) &gt; 0">
	  <h2>Test Case(s)</h2>
	  <p>
	    <table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
	      <xsl:call-template name="testcase.header.short"/>
	      <xsl:apply-templates select="$insamepackagetests" mode="print.short">
		<xsl:sort select="@orderid" order="ascending" data-type="number"/>
	      </xsl:apply-templates>
	    </table>
	  </p>
	</xsl:if>
      </body>
    </html>
  </xsl:template>


  <!--
  transform string like a.b.c to ../../../
  @param path the path to transform into a descending directory path
  -->
  <xsl:template name="path">
    <xsl:param name="path"/>
    <xsl:if test="contains($path,'/')">
      <xsl:text>../</xsl:text>    
      <xsl:call-template name="path">
	<xsl:with-param name="path"><xsl:value-of select="substring-after($path,'/')"/></xsl:with-param>
      </xsl:call-template>    
    </xsl:if>
    <xsl:if test="not(contains($path,'/')) and not($path = '')">
      <xsl:text>../</xsl:text>    
    </xsl:if>
  </xsl:template>

  <!-- create the link to the stylesheet based on the package name -->
  <xsl:template name="create.stylesheet.link">
    <xsl:param name="package.name"/>
    <link rel="stylesheet" type="text/css" title="Style"><xsl:attribute name="href"><xsl:if test="not($package.name = 'unnamed package')"><xsl:call-template name="path"><xsl:with-param name="path" select="$package.name"/></xsl:call-template></xsl:if>stylesheet.css</xsl:attribute></link>
  </xsl:template>
  
  
  <!-- Page HEADER -->
  <xsl:template name="pageHeader">
    <h1>
      Dant Test Suites<xsl:if test="not(/testsuites/@name = '')">:
	<xsl:value-of select="/testsuites/@name"/>
      </xsl:if>
    </h1>
    <table width="100%">
      <tr>
        <td align="left"></td>
        <td align="right">Designed for use with <a href="http://dant.3322.org" target="_blank">Dant</a> and <a href="http://jakarta.apache.org/" target="_blank">Ant</a>.</td>
      </tr>
    </table>
    <hr size="1"/>
  </xsl:template>

  <!-- class header -->
  <xsl:template name="testsuites.header">
    <tr valign="top">
      <th width="80%">Name</th>
      <th>Tests</th>
      <th>Failures</th>
      <th nowrap="nowrap">Time(s)</th>
    </tr>
  </xsl:template>
  
  <xsl:template name="testcase.header.short">
    <tr valign="top">
      <th>Name</th>
      <th width="10%" nowrap="nowrap">Time(s)</th>
      <th width="10%">Result</th>
    </tr>
  </xsl:template>

  <xsl:template name="testcase.header.long">
    <tr valign="top">
      <th width="20%">Name</th>
      <th>Summary</th>
      <th width="10%" nowrap="nowrap">Time(s)</th>
      <th width="10%">Result</th>
    </tr>
  </xsl:template>

  <!-- method header -->
  <xsl:template name="task.header">
    <tr valign="top">
      <th width="20%">Name</th>
      <th>Destination Host</th>
      <th width="10%" nowrap="nowrap">Time(s)</th>
      <th width="10%">Result</th>
    </tr>
  </xsl:template>
  
  
  <!-- class information -->
  <xsl:template match="testcase" mode="print.short">
    <xsl:variable name="package.name" select="@packagename"/>
    <xsl:variable name="test.name" select="@name"/>
    <tr valign="top">       
      <xsl:attribute name="class">
	<xsl:choose>
	  <xsl:when test="@result = 'FAILED'">FAILED</xsl:when>
	  <xsl:otherwise>Pass</xsl:otherwise>
	</xsl:choose>
      </xsl:attribute>
      <td><a href="{@name}.html"><xsl:value-of select="@name"/></a></td>
      <td><xsl:call-template name="display-time">
	  <xsl:with-param name="value" select="sum(../task[@packagename = $package.name and @name = $test.name and not(starts-with(@taskname,'startjavaserver')) and not(starts-with(@taskname,'startcxxserver'))]/@time)"/>
	</xsl:call-template>
      </td>
      <td><xsl:apply-templates select="@result"/></td>
    </tr>
  </xsl:template>
  
  <xsl:template match="testcase" mode="print.long">
    <xsl:variable name="package.name" select="@packagename"/>
    <xsl:variable name="test.name" select="@name"/>
    <tr valign="top">       
      <xsl:attribute name="class">
	<xsl:choose>
	  <xsl:when test="@result = 'FAILED'">FAILED</xsl:when>
	  <xsl:otherwise>Pass</xsl:otherwise>
	</xsl:choose>
      </xsl:attribute>
      <td><a href="{@name}.html"><xsl:value-of select="@name"/></a></td>
      <td><xsl:value-of select="./description"/></td>
      <td><xsl:call-template name="display-time">
	  <xsl:with-param name="value" select="sum(../task[@packagename = $package.name and @name = $test.name and not(starts-with(@taskname,'startjavaserver')) and not(starts-with(@taskname,'startcxxserver'))]/@time)"/>
	</xsl:call-template>
      </td>
      <td><xsl:apply-templates select="@result"/></td>
    </tr>
  </xsl:template>

  <xsl:template match="task" mode="print">
    <tr valign="top">
      <xsl:attribute name="class">
	<xsl:choose>
	  <xsl:when test="@result = 'FAILED'">FAILED</xsl:when>
	  <xsl:otherwise>TableRowColor</xsl:otherwise>
	</xsl:choose>
      </xsl:attribute>
      <td><a href="{@name}_{@taskname}.html"><xsl:value-of select="@taskname"/></a></td>
      
      <td><xsl:value-of select="@host"/></td>

      <td>
	<xsl:call-template name="display-time">
	  <xsl:with-param name="value" select="@time"/>
	</xsl:call-template>
      </td>
      <td><xsl:value-of select="@result"/></td>
    </tr>
  </xsl:template>

  
  <!--
  template that will convert a carriage return into a br tag
  @param word the text from which to convert CR to BR tag
  -->
  <xsl:template name="br-replace">
    <xsl:param name="word"/>
    <xsl:choose>
      <xsl:when test="contains($word,'&#xA;')">
	<xsl:value-of select="substring-before($word,'&#xA;')"/>
	<br/>
	<xsl:call-template name="br-replace">
	  <xsl:with-param name="word" select="substring-after($word,'&#xA;')"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$word"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="display-time">
    <xsl:param name="value"/>
    <xsl:value-of select="format-number($value,'0.000')"/>
  </xsl:template>

  <xsl:template name="display-percent">
    <xsl:param name="value"/>
    <xsl:value-of select="format-number($value,'0.00%')"/>
  </xsl:template>
</xsl:stylesheet>

